package com.wang.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by wangq on 2021/8/21.
 */

public class RequestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("校验session。。。。。。");
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        Object username = session.getAttribute("username");
        if (username != null) {
            System.out.println("username is:" + username);
            return true;
        } else {
            System.out.println("username is null");
            response.sendRedirect(request.getContextPath() + "/login/toLogin");
            return false;
        }
    }

}
